local SkillStrategy = class("SkillStrategy")
local GameUtils = require("utils.GameUtils")
local AttributeConst = require("const.AttributeConst")
local StrategyHelper = require("gameLogic.fightSkill.strategy.StrategyHelper")
local StrategyConst = require("gameLogic.fightSkill.strategy.StrategyConst")
local Vector3 = require("utils.Vector3")
-- 技能效果策略

function SkillStrategy:ctor(skill, config)
    self._skill = skill
    self._config = config
end

function SkillStrategy:doStrategy(target)
    self:_callStrategy(target)
end

function SkillStrategy:_callStrategy(target)
    local funcName = self._config.hurt
    local strategyFunc = self["_strategy_" .. funcName]
    if strategyFunc and type(strategyFunc) == "function" then
        strategyFunc(self, target)
        self:_addBuff(target)
    end
end

function SkillStrategy:getTime()
    return self._config.effect_time
end

function SkillStrategy:_addBuff(target)
    if self._config.buff > 0 then
        local buff = require("config.buff").get(self._config.buff)
        local buffManager = target:getManager():getBuffManager()
        local randomMap = G_UserData:getRandomList(self._config.probability)
        local rondomValue = math.random(1, 100)
        local myEntity = self._skill:getEntity()
        if rondomValue <= self._config.probability then
            buffManager:addBuff(buff, target, myEntity)
        end
    end
end

-- 物理伤害策略
function SkillStrategy:_strategy_simple(target)
    local hurt, isCrit =
        StrategyHelper.simpleHurt(self._skill:getEntity():getAttr(), target:getAttr(), self._config.hurt_ratio)
    target:beAttack(hurt)
    G_SignalManager:dispatch(
        SignalConst.EVENT_FIGHT_HURT_TEXT,
        {parent = target:getBloodUI(), text = hurt, isCrit = isCrit, element = 0}
    )
end

-- 元素伤害策略
function SkillStrategy:_strategy_element(target)
    printDebug("" .. self._skill:getEntity():getShowName() .. " " .. self._skill:getEntity():getElement())
    local hurt, isCrit =
        StrategyHelper.elementHurt(
        self._skill:getEntity():getAttr(),
        target:getAttr(),
        self._config.hurt_ratio,
        self._skill:getEntity():getElement()
    )
    target:beAttack(hurt)
    G_SignalManager:dispatch(
        SignalConst.EVENT_FIGHT_HURT_TEXT,
        {parent = target:getBloodUI(), text = hurt, isCrit = isCrit, element = self._skill:getEntity():getElement()}
    )
end

-- 反击伤害策略
function SkillStrategy:_strategy_counterattack(target)
    local hurt = StrategyHelper.counterattackHurt(self._skill:getEntity():getAttr(), target:getAttr())
    target:beAttack(hurt)
end

-- 治疗策略
function SkillStrategy:_strategy_treatment(target)
    local attr = self._skill:getEntity():getAttr()
    local hurt = math.floor(attr:getAttrValue(AttributeConst.ATTACK) * self._config.hurt_ratio)
    hurt, isCrit = StrategyHelper.critHurt(attr, hurt)
    target:subAttrValue(AttributeConst.BLOOD, -hurt)
    G_SignalManager:dispatch(
        SignalConst.EVENT_FIGHT_HURT_TEXT,
        {
            parent = target:getBloodUI(),
            text = hurt,
            isCrit = isCrit,
            element = AttributeConst.ELEMENT_TREATMENT
        }
    )
end

return SkillStrategy
